package cn.changgou.search.controller;

import cn.changgou.entity.Page;
import cn.changgou.search.feign.SkuFeign;
import cn.changgou.search.pojo.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@Controller
@RequestMapping("/search")
public class SkuController {

    @Autowired
    private SkuFeign skuFeign;

    /*实现搜索调用*/
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String search(@RequestParam(required = false) Map<String, String> searchMap, Model model){
        //调用搜索微服务
        Map<String, Object> resultMap = skuFeign.search(searchMap);
        model.addAttribute("result", resultMap);

        //计算分页
        Page<SkuInfo> pageInfo = new Page<SkuInfo>(
                Long.parseLong(resultMap.get("total").toString()),
                Integer.parseInt(resultMap.get("pageNumber").toString())+1,
                Integer.parseInt(resultMap.get("pageSize").toString())
        );
        model.addAttribute("pageInfo", pageInfo);

        //将条件存储用于页面回显数据
        model.addAttribute("searchMap", searchMap);
        //获取上次请求地址
        String[] urls = url(searchMap);
        model.addAttribute("url", urls[0]);
        model.addAttribute("sortUrl", urls[1]);
        return "search";
    }

    /*获取用户每次请求的地址
    * 页面需要在这次请求的地址上添加额外的搜索条件*/
    public String[] url(Map<String, String> searchMap){
        String url = "/search/list";//初始化地址
        String sortUrl = "/search/list";//排序地址
        if (searchMap != null && searchMap.size() > 0){
            url += "?";
            sortUrl += "?";
            for (Map.Entry<String, String> entry : searchMap.entrySet()) {
                //key是搜索的条件对象
                String key = entry.getKey();
                //跳过分页参数
                if (key.equalsIgnoreCase("pageNum")){
                    continue;
                }

                //value搜索的值
                String value = entry.getValue();
                url += key + "=" + value + "&";
                //排序参数，跳过
                if (key.equalsIgnoreCase("sortField")||key.equalsIgnoreCase("sortRule")){
                    continue;
                }
                sortUrl += key + "=" + value + "&";
            }
            //去掉最后一个&
            url = url.substring(0, url.length() - 1);
            sortUrl = sortUrl.substring(0, sortUrl.length() - 1);
        }
        return new String[]{url, sortUrl};
    }
}
